package com.socool.sinofaith.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.socool.sinofaith.R;
import com.socool.sinofaith.util.SinofaithUtil;

/**
 * @author author:Reeves
 * @version 创建时间：2015-11-30 上午10:10:14 类说明
 */
public class TitlePopup extends PopupWindow {

	private Context context;
	// 屏幕的宽度和高度
	private int mScreenWidth, mScreenHeight;
	// 定义列表对象
	private ListView lv_title;
	// 坐标的位置(x,y)
	private final int[] mLocation = new int[2];
	// 实例化一个矩形
	private Rect mRect = new Rect();

	// 列表弹窗的间隔
	protected final int LIST_PADDING = 10;

	// 位置不在中心
	private int popupGravity = Gravity.NO_GRAVITY;

	// 弹窗子类项选中时的监听
	private OnPopupItemClickListener mPopupItemClickListener;;


	public TitlePopup(Context context) {
		this(context, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	}

	public TitlePopup(Context context, int width, int height) {
		this.context = context;
		// 设置可以获得焦点
		setFocusable(true);
		// 设置弹窗内可点击
		setTouchable(true);
		// 设置弹窗外可点击;
		setOutsideTouchable(true);
		// 获得屏幕的宽度和高度

		mScreenWidth = SinofaithUtil.getScreenWidth(context);
		mScreenHeight = SinofaithUtil.getScreenHeight(context);
		// 设置弹窗的宽度和高度
		setWidth(width);
		setHeight(height);
		setBackgroundDrawable(new BitmapDrawable());
		setContentView(LayoutInflater.from(context).inflate(
				R.layout.title_popup, null));
		initUI();

	}

	/**
	 * 初始化弹窗列表
	 */
	private void initUI() {
		lv_title = (ListView) getContentView()
				.findViewById(R.id.lv_title_popup);

		lv_title.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dismiss();
				if (mPopupItemClickListener != null) {
					mPopupItemClickListener.onItemClick(position);
				}

			}

		});
	}

	/**
	 * 显示弹窗列表界面
	 * 
	 * @param view
	 */
	public void show(View view,ArrayList<String> mItems) {
		// 获得点击屏幕的坐标位置
		view.getLocationOnScreen(mLocation);
		// 设置矩形的大小
		mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
				mLocation[1] + view.getHeight());
		// 判断是否需要添加或更新列表子类项

		populateActions( mItems);

		showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING
				- (getWidth() / 2), mRect.bottom);
	} 

	/**
	 * 设置弹窗列表子项
	 */
	private void populateActions(final ArrayList<String> mItems) {
		// 设置列表适配器
		lv_title.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ViewHolder holder = null;
				if (convertView == null) {
					holder = new ViewHolder();
					convertView = View.inflate(context, R.layout.item_popup, null);
					holder.tv = (TextView) convertView
							.findViewById(R.id.tv_right_item_popup);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				holder.tv.setText(mItems.get(position));
				return convertView;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return position;
			}

			@Override
			public Object getItem(int position) {
				return mItems.get(position);
			}

			@Override
			public int getCount() {
				return mItems.size();
			}
		});

	}

	public void setOnPoPupItemClickListener(
			OnPopupItemClickListener onItemClickListener) {
		this.mPopupItemClickListener = onItemClickListener;
	}

	public interface OnPopupItemClickListener {
		public void onItemClick(int position);
	}
	private class ViewHolder {
		TextView tv;
	}
}
